package com.devbobcorn.donutopia.common;

import java.util.Set;

import com.devbobcorn.donutopia.Donutopia;
import com.devbobcorn.donutopia.blocks.ModBlocks;
import com.devbobcorn.donutopia.entities.ModEntityTypes;
import com.devbobcorn.donutopia.items.ModItemTab;
import com.google.common.collect.ImmutableSet;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.world.gen.carver.WorldCarver;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DeferredWorkQueue;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.registries.IForgeRegistry;

@EventBusSubscriber(modid = Donutopia.MODID, bus = EventBusSubscriber.Bus.MOD)
public class CommonModEventSubscriber {
    private static final Logger LOGGER = LogManager.getLogger(Donutopia.MODID + " Mod Event Subscriber");

	/**
	 * This method will be called by Forge when it is time for the mod to register
	 * its Items. This method will always be called after the Block registry method.
	 */
	@SubscribeEvent
	public static void onRegisterItems(final RegistryEvent.Register<Item> event) {
		final IForgeRegistry<Item> registry = event.getRegistry();

		// Automatically register BlockItems for all our Blocks
		ModBlocks.BLOCKS.getEntries().stream().map(RegistryObject::get)
			// You can do extra filtering here if you don't want some blocks to have an
			// BlockItem automatically registered for them
			// .filter(block -> needsItemBlock(block))
			// Register the BlockItem for the block
			.forEach(block -> {
                Item.Properties properties = new Item.Properties().tab(ModItemTab.DONUTOPIA_GROUP);
                BlockItem blockItem = new BlockItem(block, properties);
				//Set its Registry Name...
				blockItem.setRegistryName(block.getRegistryName());
				//And register it...
				registry.register(blockItem);
			});
		LOGGER.info("BlockItems Registered.");

		ModEntityTypes.registerSpawnEggs(event);
		LOGGER.info("Spawn Eggs Registered.");
	}

	@SubscribeEvent
	@SuppressWarnings("all")
	public void init(FMLCommonSetupEvent event) {
		DeferredWorkQueue.runLater(() -> {
			try {
				ObfuscationReflectionHelper.setPrivateValue(WorldCarver.class, WorldCarver.CAVE, new ImmutableSet.Builder<Block>()
						.addAll((Set<Block>) ObfuscationReflectionHelper.getPrivateValue(WorldCarver.class, WorldCarver.CAVE, "field_222718_j"))
						.add(Blocks.CRYING_OBSIDIAN.defaultBlockState().getBlock()).build(), "field_222718_j");
				ObfuscationReflectionHelper.setPrivateValue(WorldCarver.class, WorldCarver.CANYON, new ImmutableSet.Builder<Block>()
						.addAll((Set<Block>) ObfuscationReflectionHelper.getPrivateValue(WorldCarver.class, WorldCarver.CANYON, "field_222718_j"))
						.add(Blocks.CRYING_OBSIDIAN.defaultBlockState().getBlock()).build(), "field_222718_j");
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
	}
}
